package com.sec.android.app.myfiles.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.View;

import com.sec.android.app.myfiles.R;

/**
 * Created by jaebae on 2016-05-12.
 */
public class DrawableMgr {
    private static SparseArray<Drawable> sIconDrawable = new SparseArray<>();
    private static SparseArray<Drawable> sFileIconDrawable = new SparseArray<>();

    public static Drawable getDrawable(Context context, int resId) {
        Drawable ret = sIconDrawable.get(resId);
        if (ret == null && resId != View.NO_ID) {
            ret = context.getDrawable(resId);
            if (ret != null) {
                sIconDrawable.put(resId, ret);
            }
        }
        return ret;
    }

    private static int sEnlargeIconSize = -1;

    public static int getEnlargeSize(Context context) {
        if (sEnlargeIconSize < 0) {
            sEnlargeIconSize = context.getApplicationContext().getResources().getDimensionPixelSize(R.dimen.enlarge_icon_size);
        }
        return sEnlargeIconSize;
    }

    public static Drawable getFileIconDrawable(Context context, int resId) {
        if (resId == View.NO_ID || context == null) {
            return null;
        }

        Drawable ret = sFileIconDrawable.get(resId);
        if (ret != null) {
            return ret;
        }

        Drawable drawable = getDrawable(context.getApplicationContext(), resId);

        Bitmap oriBmp = getBitmapFromDrawable(drawable);
        if (resId == R.drawable.myfiles_list_folder || resId == R.drawable.myfiles_list_folder_3rd) {
            int fileIconSize = (int) (oriBmp.getWidth() * 38.5 / 36);
            Bitmap targetBmp = Bitmap.createBitmap(fileIconSize, fileIconSize, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(targetBmp);
            Paint paint = fillFolderTint(context, resId);
            canvas.drawBitmap(oriBmp, 0, 0, paint);
            canvas.save();
            oriBmp = targetBmp;
        }

        ret = new BitmapDrawable(context.getResources(), oriBmp);
        sFileIconDrawable.put(resId, ret);

        return ret;
    }

    private static Paint fillFolderTint(Context context, int resId) {
        Paint ret = null;
        if (resId == R.drawable.myfiles_list_folder || resId == R.drawable.myfiles_list_folder_3rd) {
            ret = new Paint();
            ret.setColorFilter(new PorterDuffColorFilter(context.getResources().getColor(R.color.myfiles_list_folder_icon_tint, null), PorterDuff.Mode.SRC_IN));
        }
        return ret;
    }

    public static Bitmap getBitmapFromDrawable(Drawable drawable) {
        Bitmap ret;
        if (drawable instanceof BitmapDrawable) {
            ret = ((BitmapDrawable) drawable).getBitmap();
        } /*else if (drawable instanceof SemPathRenderingDrawable) {
            ret = ((SemPathRenderingDrawable) drawable).getBitmap();
        }*/ else {
            /*
             * 3rd Party App icon might have other drawable types.
             * For example, Facebook App icon is LayerDrawable object.
             * So, below these codes are needed.
            * */
            int width = drawable.getIntrinsicWidth();
            int height = drawable.getIntrinsicHeight();
            Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            ret = bitmap;
        }
        return ret;
    }

    public static void clearIconDrawables() {
        sIconDrawable.clear();
        sFileIconDrawable.clear();
    }
}
